package com.hoperun.product.activity;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.hoperun.product.Models.DaoshaModel;
import com.hoperun.product.view.SimpleDialog;
import com.hoperun.yieldmgr.R;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by wgy on 17/5/19.
 */
public class EditDialog extends Dialog {

    public EditDialog(Context context) {
        super(context);
    }

    public EditDialog(Context context, int theme) {
        super(context, theme);
    }

    public static class Builder {
        QuickAdapter quickAdapter;
         List<DaoshaModel> selectinputlist=new ArrayList<DaoshaModel>();
        List<DaoshaModel> inputlist=new ArrayList<DaoshaModel>();
        private Activity context;
        private String title;
        private String positiveButtonText;
        private String negativeButtonText;
        private View contentView;
        private OnClickListener positiveButtonClickListener;
        private OnClickListener negativeButtonClickListener;

        public Builder(Activity context) {
            this.context = context;
        }


        public Builder setContentView(View v) {
            this.contentView = v;
            return this;
        }

        public Builder setPositiveButton(int positiveButtonText,
                                         OnClickListener listener) {
            this.positiveButtonText = (String) context
                    .getText(positiveButtonText);
            this.positiveButtonClickListener = listener;
            return this;
        }

        public  void setDaoList(List<DaoshaModel> list){
             this.inputlist.addAll(list);
             quickAdapter.notifyDataSetChanged();
        }

        public Builder setPositiveButton(String positiveButtonText,
                                         OnClickListener listener) {
            this.positiveButtonText = positiveButtonText;
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(int negativeButtonText,
                                         OnClickListener listener) {
            this.negativeButtonText = (String) context
                    .getText(negativeButtonText);
            this.negativeButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(String negativeButtonText,
                                         OnClickListener listener) {
            this.negativeButtonText = negativeButtonText;
            this.negativeButtonClickListener = listener;
            return this;
        }

        public EditDialog create() {

            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // instantiate the dialog with the custom Theme
            final EditDialog dialog = new EditDialog(context, R.style.Dialog);
            dialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失
            View layout = inflater.inflate(R.layout.edit_dialog, null);
            dialog.addContentView(layout, new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

            RecyclerView dialog_recycler=(RecyclerView)layout.findViewById(R.id.dialog_recylistview);
            LinearLayoutManager layoutManager = new LinearLayoutManager(context);
            layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            dialog_recycler.setLayoutManager(layoutManager);
            //给RecyclerView设置适配器
            quickAdapter = new QuickAdapter();
            dialog_recycler.setAdapter(quickAdapter);


            if (positiveButtonText != null) {
                ((Button) layout.findViewById(R.id.positiveButton))
                        .setText(positiveButtonText);
                if (positiveButtonClickListener != null) {
                    ((Button) layout.findViewById(R.id.positiveButton))
                            .setOnClickListener(new View.OnClickListener() {
                                public void onClick(View v) {
                                    positiveButtonClickListener.onClick(dialog,
                                            DialogInterface.BUTTON_POSITIVE);
                                }
                          });
                }
            } else {
                // if no confirm button just set the visibility to GONE
                layout.findViewById(R.id.positiveButton).setVisibility(
                        View.GONE);
            }
            // set the cancel button
            if (negativeButtonText != null) {
                ((Button) layout.findViewById(R.id.negativeButton))
                        .setText(negativeButtonText);
                if (negativeButtonClickListener != null) {
                    ((Button) layout.findViewById(R.id.negativeButton))
                            .setOnClickListener(new View.OnClickListener() {
                                public void onClick(View v) {
                                    negativeButtonClickListener.onClick(dialog,
                                            DialogInterface.BUTTON_NEGATIVE);
                                }
                            });
                }
            } else {
                // if no confirm button just set the visibility to GONE
                layout.findViewById(R.id.negativeButton).setVisibility(
                        View.GONE);
            }
//          if (contentView != null) {
//                // if no message set
//                // add the contentView to the dialog body
//                ((LinearLayout) layout.findViewById(R.id.content))
//                        .removeAllViews();
//                ((LinearLayout) layout.findViewById(R.id.content)).addView(
//                        contentView, new ViewGroup.LayoutParams(
//                                ViewGroup.LayoutParams.FILL_PARENT,
//                                ViewGroup.LayoutParams.FILL_PARENT));
//            }

//            if(mydata!=null){//输入验证码的
//                getcode();
//            }else{//离职获取验证码
//              getResigncode();
//            }
            dialog.setContentView(layout);
            return dialog;
        }



        public class QuickAdapter extends BaseQuickAdapter<DaoshaModel, BaseViewHolder> {
            public QuickAdapter() {
                super(R.layout.edit_dialog_item, inputlist);
            }

            @Override
            protected void convert(final BaseViewHolder holder, DaoshaModel item) {
                TextView item_quan = holder.getView(R.id.item_quan);//全称
                TextView item_sehao = holder.getView(R.id.item_sehao);//色号
                TextView item_pihao = holder.getView(R.id.item_pihao);//批号
                CheckBox input_item_checkBox = holder.getView(R.id.input_item_checkBox);

                input_item_checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        inputlist.get(holder.getLayoutPosition()).setBoxcheck(isChecked);
                    }
                });

                item_sehao.setText(item.getColorno()==null?"":item.getColorno());
                item_quan.setText(item.getSzqc()==null?"":item.getSzqc());
                item_pihao.setText(item.getBatchno()==null?"":item.getBatchno());
                input_item_checkBox.setChecked(item.isBoxcheck());

            }
        }

    }

}
